package cn.psh.basic.controller;


import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public  class FastDfsController {
    //文件上传
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file", required = true) MultipartFile file) {
        String filename = file.getOriginalFilename();
        String extName = filename.substring(filename.lastIndexOf(".") + 1);
        try {
            String path = FastDfsUtils.upload(file.getBytes(), extName);
            System.out.println(path);
            return AjaxResult.OJBK().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统繁忙，稍后重试f！！！");
        }
    }
    //文件删除接口
    @DeleteMapping
    public AjaxResult delete(@RequestParam(value = "path",required = true) String path){
        try {
            String pathTmp = path.substring(1);
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/"));
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);
            System.out.println(groupName);
            System.out.println(remotePath);
            FastDfsUtils.delete(groupName,remotePath);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("删除失败！"+e.getMessage());
        }
    }
}
